package com.techsoul.module.tms.service.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.tms.mapper.CarrierMapper;
import com.techsoul.module.tms.pojo.entity.Carrier;
import com.techsoul.module.tms.pojo.param.CarrierSaveParam;
import com.techsoul.module.tms.pojo.param.CarrierSearchParam;
import com.techsoul.module.tms.pojo.vo.CarrierVo;
import com.techsoul.module.tms.service.ICarrierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author zhaowei
 * @since 2021-06-02
 */
@DS("tms")
@Slf4j
@Service
public class TsCarrierServiceImpl  implements ICarrierService {
    @Autowired
    private CarrierMapper mapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TokenService tokenService;

    public String insert(CarrierSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        Carrier entity = new Carrier();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        mapper.insert(entity);
        return entity.getId();
    }

    public Integer delete(String id){return this.mapper.deleteById(id);
    }

    public void update(String id, CarrierSaveParam params) {
        Carrier entity = new Carrier();
        BeanUtils.copyProperties(params, entity);
        entity.setId(id);
        mapper.updateById(entity);
    }

    public CarrierVo findById(String id) {
        return mapper.findById(id);
    }

    public List<Carrier> findAll() {

        return mapper.selectList(new QueryWrapper<>());
    }

    public List<CarrierVo> findAll(CarrierSearchParam params) {
        return mapper.findAll(params);
    }

    public IPage<CarrierVo> findAll(CarrierSearchParam params, Integer currentPage, Integer pageSize) {
        Page<CarrierVo> page = new Page<>(currentPage, pageSize);
        page.addOrder(OrderItem.desc("t1.update_date"));
        return mapper.findAll(page, params);
    }

}
